#ifndef INDEX_BUFFER_H
#define INDEX_BUFFER_H

template <typename IndexType>
class IndexBuffer
{
public:
    // Constructor for creating buffer
    IndexBuffer();
    // Constructor for creating buffer with passing pre-generated VBO id
    IndexBuffer(GLuint buffer);
    ~IndexBuffer();
    
    void SetData(std::vector<IndexType> indices);
	  void SetEmpty();

    void SetBufferId(GLuint buffer);    

    void Enable();
    void Disable();

    int GetIndicesNumber();

private:
    GLuint bufferId;
    int indicesNumber;
};

template <typename IndexType>
IndexBuffer<IndexType>::IndexBuffer()
{    
    glGenBuffers(1, &bufferId);
}

template <typename IndexType>
IndexBuffer<IndexType>::~IndexBuffer()
{
    glDeleteBuffers(1, &bufferId);
}

template <typename IndexType>
IndexBuffer<IndexType>::IndexBuffer(GLuint buffer)
{
    bufferId = buffer;
}

template <typename IndexType>
void IndexBuffer<IndexType>::SetData(std::vector<IndexType> indices)
{    
	SetEmpty();
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(IndexType), &indices[0], GL_STATIC_DRAW);
	indicesNumber = indices.size();
}

template <typename IndexType>
void IndexBuffer<IndexType>::SetEmpty()
{
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, 0, 0, GL_STATIC_DRAW);
	indicesNumber = 0;
}

template <typename IndexType>
void IndexBuffer<IndexType>::SetBufferId(GLuint buffer)
{
    bufferId = buffer;
}

template <typename IndexType>
void IndexBuffer<IndexType>::Enable()
{
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferId);
}

template <typename IndexType>
void IndexBuffer<IndexType>::Disable()
{
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}

template <typename IndexType>
int IndexBuffer<IndexType>::GetIndicesNumber()
{
    return indicesNumber;
}

#endif